package com.yanqu.road.dao.impl.activity.biaoju;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.biaoju.BiaoJuTeamUserInfo;
import com.yanqu.road.entity.activity.biaoju.CrossBiaoJuUnionData;
import com.yanqu.road.entity.activity.biaoju.CrossBiaoJuUserRunEscortData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossBiaoJuUserRunEscortImpl extends TempDataObjectDao<CrossBiaoJuUserRunEscortData> {
    public static String getCrossBiaoJuUserRunEscortDataSql = "select * from `t_c_biaoju_user_run` where `activityId`=? and `unionGroup`=?";

    public Map<Long, List<CrossBiaoJuUserRunEscortData>> getCrossBiaoJuUserRunEscortData(int activityId, int unionGroup) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, List<CrossBiaoJuUserRunEscortData>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossBiaoJuUserRunEscortDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossBiaoJuUserRunEscortData data = parse(rs);
                List<CrossBiaoJuUserRunEscortData> list = dataMap.get(data.getUserId());
                if (list == null) {
                    list = new ArrayList<>();
                    dataMap.put(data.getUserId(), list);
                }
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateCrossBiaoJuUserRunEscortDataSql = "update `t_c_biaoju_user_run` set round=?,bestEscortId=?,escortId=?,cityId=?,battleValue=?,remainBlood=?,takeTime=?,runStartTime=?,finishTime=?,speedUpTime=?,teamUserList=?,battleLogList=?,unionLevel=?,userLevel=?,cityAddition=?,reward=?,leaderReward=? where activityId=? and userId=? and unionGroup=? and uniqueId=?";
    @Override
    protected boolean updateData(CrossBiaoJuUserRunEscortData crossBiaoJuUserRunEscortData) {
        crossBiaoJuUserRunEscortData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getBestEscortId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getEscortId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getCityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getBattleValue()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getRemainBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserRunEscortData.getTakeTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserRunEscortData.getRunStartTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserRunEscortData.getFinishTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserRunEscortData.getSpeedUpTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuUserRunEscortData.getTeamUserList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuUserRunEscortData.getBattleLogList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getUnionLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getUserLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getCityAddition()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossBiaoJuUserRunEscortData.getReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossBiaoJuUserRunEscortData.getLeaderReward()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserRunEscortData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getUniqueId()));
        if (getDb().executeNoneQuery(updateCrossBiaoJuUserRunEscortDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossBiaoJuUserRunEscortDataSql = "insert into `t_c_biaoju_user_run`(activityId,userId,unionGroup,uniqueId,round,bestEscortId,escortId,cityId,battleValue,remainBlood,takeTime,runStartTime,finishTime,speedUpTime,teamUserList,battleLogList,unionLevel,userLevel,cityAddition,reward,leaderReward) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossBiaoJuUserRunEscortData crossBiaoJuUserRunEscortData) {
        crossBiaoJuUserRunEscortData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserRunEscortData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getUniqueId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getBestEscortId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getEscortId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getCityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getBattleValue()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getRemainBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserRunEscortData.getTakeTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserRunEscortData.getRunStartTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserRunEscortData.getFinishTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossBiaoJuUserRunEscortData.getSpeedUpTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuUserRunEscortData.getTeamUserList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossBiaoJuUserRunEscortData.getBattleLogList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getUnionLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getUserLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, crossBiaoJuUserRunEscortData.getCityAddition()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossBiaoJuUserRunEscortData.getReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossBiaoJuUserRunEscortData.getLeaderReward()));
        if (getDb().executeNoneQuery(insertCrossBiaoJuUserRunEscortDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossBiaoJuUserRunEscortData parseData(ResultSet rs) throws SQLException {
        CrossBiaoJuUserRunEscortData crossBiaoJuUserRunEscortData = new CrossBiaoJuUserRunEscortData();
        crossBiaoJuUserRunEscortData.setActivityId(rs.getInt("activityId"));
        crossBiaoJuUserRunEscortData.setUserId(rs.getLong("userId"));
        crossBiaoJuUserRunEscortData.setUnionGroup(rs.getInt("unionGroup"));
        crossBiaoJuUserRunEscortData.setUniqueId(rs.getInt("uniqueId"));
        crossBiaoJuUserRunEscortData.setRound(rs.getInt("round"));
        crossBiaoJuUserRunEscortData.setBestEscortId(rs.getInt("bestEscortId"));
        crossBiaoJuUserRunEscortData.setEscortId(rs.getInt("escortId"));
        crossBiaoJuUserRunEscortData.setCityId(rs.getInt("cityId"));
        crossBiaoJuUserRunEscortData.setBattleValue(rs.getInt("battleValue"));
        crossBiaoJuUserRunEscortData.setRemainBlood(rs.getInt("remainBlood"));
        crossBiaoJuUserRunEscortData.setTakeTime(rs.getLong("takeTime"));
        crossBiaoJuUserRunEscortData.setRunStartTime(rs.getLong("runStartTime"));
        crossBiaoJuUserRunEscortData.setFinishTime(rs.getLong("finishTime"));
        crossBiaoJuUserRunEscortData.setSpeedUpTime(rs.getLong("speedUpTime"));
        crossBiaoJuUserRunEscortData.setTeamUserList(JSON.parseObject(rs.getString("teamUserList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.biaoju.BiaoJuTeamUserInfo>>() {}));
        crossBiaoJuUserRunEscortData.setBattleLogList(JSON.parseObject(rs.getString("battleLogList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.biaoju.BiaoJuEscortBattleLog>>() {}));
        crossBiaoJuUserRunEscortData.setUnionLevel(rs.getInt("unionLevel"));
        crossBiaoJuUserRunEscortData.setUserLevel(rs.getInt("userLevel"));
        crossBiaoJuUserRunEscortData.setCityAddition(rs.getInt("cityAddition"));
        crossBiaoJuUserRunEscortData.setReward(rs.getString("reward"));
        crossBiaoJuUserRunEscortData.setLeaderReward(rs.getString("leaderReward"));

        return crossBiaoJuUserRunEscortData;
    }
}
